package com.smartinput.device.controller

import com.mensanyun.kotool.util.executeQueryWithTry
import com.mensanyun.kotool.util.executeWithTry
import com.smartinput.device.application.dto.DeviceRegisterRequest
import com.smartinput.device.application.dto.DeviceStatusUpdateRequest
import com.smartinput.device.application.service.DeviceService
import jakarta.validation.Valid
import org.springframework.security.core.context.SecurityContextHolder
import org.springframework.security.core.userdetails.UserDetails
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/api/devices")
class DeviceController(
    private val deviceService: DeviceService
) {

    @PostMapping("/register")
    fun registerDevice(@Valid @RequestBody request: DeviceRegisterRequest) = executeWithTry {
        val principal = SecurityContextHolder.getContext().authentication.principal
        val currentUserId = if (principal is UserDetails) {
            principal.username
        } else {
            principal.toString()
        }

        // 创建新的请求对象，使用当前登录用户的ID
        val authenticatedRequest = DeviceRegisterRequest(
            deviceId = request.deviceId,
            deviceName = request.deviceName,
            deviceType = request.deviceType,
            userId = currentUserId
        )

        deviceService.registerDevice(authenticatedRequest)
    }

    @GetMapping
    fun getUserDevices() = executeQueryWithTry {
        val principal = SecurityContextHolder.getContext().authentication.principal
        val currentUserId = if (principal is UserDetails) {
            principal.username
        } else {
            principal.toString()
        }
        deviceService.getUserDevices(currentUserId)
    }

    @GetMapping("/online")
    fun getOnlineDevices() = executeQueryWithTry {
        val principal = SecurityContextHolder.getContext().authentication.principal
        val currentUserId = if (principal is UserDetails) {
            principal.username
        } else {
            principal.toString()
        }
        deviceService.getOnlineDevices(currentUserId)
    }

    @PostMapping("/{deviceId}/heartbeat")
    fun heartbeat(@PathVariable deviceId: String) = executeWithTry {
        deviceService.heartbeat(deviceId)
    }

    @PutMapping("/{deviceId}/status")
    fun updateDeviceStatus(
        @PathVariable deviceId: String,
        @Valid @RequestBody request: DeviceStatusUpdateRequest
    ) = executeWithTry {
        deviceService.updateDeviceStatus(deviceId, request.online, request.socketId)
    }

    @PostMapping("/{deviceId}/claim")
    fun claimAnonymousDevice(@PathVariable deviceId: String) = executeWithTry {
        val principal = SecurityContextHolder.getContext().authentication.principal
        val currentUserId = if (principal is UserDetails) {
            principal.username
        } else {
            principal.toString()
        }

        deviceService.claimAnonymousDevice(deviceId, currentUserId)
    }
}